import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import "qrc:/qml/styles" as Styles

Rectangle {
    id: root
    property string chainId: ""
    property string taskName: ""
    property int agvNumber: -1
    property string agvName: ""
    property string status: "pending"
    property string statusDisplay: ""
    property int currentTaskIndex: 0
    property int totalTasks: 0
    property int progress: 0
    property string createTime: ""
    property string errorMessage: ""
    property string currentSubTask: ""
    property string currentSubTaskType: ""
    property string targetStation: ""
    property int currentFloor: -1
    property int durationSeconds: 0
    property bool isPaused: false
    property bool isFinished: false

    signal clicked()

    width: 320
    implicitHeight: content.implicitHeight + Styles.Theme.spacingNormal * 2
    color: mouseArea.pressed ? Qt.darker(Styles.Theme.surface, 1.08) :
           mouseArea.containsMouse ? Qt.lighter(Styles.Theme.surface, 1.03) : Styles.Theme.surface
    radius: Styles.Theme.radiusNormal
    border.color: mouseArea.containsMouse ? Styles.Theme.primary : Styles.Theme.border
    border.width: 1

    // 左侧状态指示条
    Rectangle {
        id: statusIndicator
        width: 4
        height: parent.height - 2
        anchors.left: parent.left
        anchors.leftMargin: 1
        anchors.verticalCenter: parent.verticalCenter
        radius: 2
        color: statusColor(root.status)
    }

    // 平滑过渡动画
    Behavior on color {
        ColorAnimation { duration: 150 }
    }
    Behavior on border.color {
        ColorAnimation { duration: 150 }
    }
    Behavior on scale {
        NumberAnimation { duration: 100; easing.type: Easing.OutQuad }
    }

    scale: mouseArea.pressed ? 0.98 : 1.0
    transformOrigin: Item.Center

    MouseArea {
        id: mouseArea
        anchors.fill: parent
        hoverEnabled: true
        cursorShape: Qt.PointingHandCursor
        onClicked: root.clicked()
    }

    ColumnLayout {
        id: content
        anchors.fill: parent
        anchors.leftMargin: Styles.Theme.spacingNormal + 8
        anchors.rightMargin: Styles.Theme.spacingNormal
        anchors.topMargin: Styles.Theme.spacingNormal
        anchors.bottomMargin: Styles.Theme.spacingNormal
        spacing: Styles.Theme.spacingNormal

        // 头部：任务名称和状态
        RowLayout {
            Layout.fillWidth: true
            spacing: Styles.Theme.spacingSmall

            ColumnLayout {
                Layout.fillWidth: true
                spacing: 4

                Text {
                    text: taskName.length > 0 ? taskName : chainId
                    color: Styles.Theme.textPrimary
                    font.pixelSize: Styles.Theme.fontSizeNormal
                    font.weight: Font.DemiBold
                    elide: Text.ElideRight
                    Layout.fillWidth: true
                }

                Text {
                    text: agvNumber > 0 ? (agvName.length > 0 ? agvName : qsTr("AGV #") + agvNumber) : qsTr("未分配")
                    color: agvNumber > 0 ? Styles.Theme.textSecondary : Styles.Theme.warning
                    font.pixelSize: Styles.Theme.fontSizeSmall
                    Layout.fillWidth: true
                    elide: Text.ElideRight
                    opacity: 0.8
                }
            }

            // 状态标签
            Rectangle {
                width: statusLabelText.width + 16
                height: 24
                radius: 12
                color: Qt.alpha(statusColor(root.status), 0.15)

                Text {
                    id: statusLabelText
                    anchors.centerIn: parent
                    text: statusDisplay.length > 0 ? statusDisplay : getStatusText()
                    color: statusColor(root.status)
                    font.pixelSize: 11
                    font.weight: Font.Medium
                }
            }
        }

        // 当前子任务
        Rectangle {
            visible: currentSubTask.length > 0
            Layout.fillWidth: true
            height: subTaskRow.height + Styles.Theme.spacingSmall * 2
            color: Qt.alpha(Styles.Theme.backgroundAlternate, 0.5)
            radius: Styles.Theme.radiusSmall

            RowLayout {
                id: subTaskRow
                anchors.fill: parent
                anchors.margins: Styles.Theme.spacingSmall
                spacing: Styles.Theme.spacingSmall

                Text {
                    text: currentSubTask
                    color: Styles.Theme.textPrimary
                    font.pixelSize: Styles.Theme.fontSizeSmall
                    wrapMode: Text.WordWrap
                    Layout.fillWidth: true
                }

                // 目标站点
                Rectangle {
                    visible: targetStation.length > 0
                    width: targetText.width + 12
                    height: 20
                    radius: 10
                    color: Qt.alpha(Styles.Theme.primary, 0.15)

                    Text {
                        id: targetText
                        anchors.centerIn: parent
                        text: targetStation
                        color: Styles.Theme.primary
                        font.pixelSize: 10
                        font.weight: Font.Medium
                    }
                }
            }
        }

        // 进度条
        ColumnLayout {
            Layout.fillWidth: true
            spacing: 6

            RowLayout {
                Layout.fillWidth: true

                Text {
                    text: qsTr("进度")
                    color: Styles.Theme.textSecondary
                    font.pixelSize: 10
                    opacity: 0.7
                }

                Item { Layout.fillWidth: true }

                Text {
                    text: totalTasks > 0 ? (currentTaskIndex + "/" + totalTasks) : (progress + "%")
                    color: Styles.Theme.textSecondary
                    font.pixelSize: 10
                    font.weight: Font.Medium
                }
            }

            // 自定义进度条
            Rectangle {
                Layout.fillWidth: true
                height: 6
                radius: 3
                color: Qt.alpha(Styles.Theme.border, 0.5)

                Rectangle {
                    width: parent.width * progress / 100
                    height: parent.height
                    radius: 3
                    color: statusColor(root.status)

                    Behavior on width {
                        NumberAnimation { duration: 300; easing.type: Easing.OutQuad }
                    }
                }
            }
        }

        // 底部信息行
        RowLayout {
            Layout.fillWidth: true
            spacing: Styles.Theme.spacingNormal

            // 创建时间
            Text {
                text: createTime || "-"
                color: Styles.Theme.textSecondary
                font.pixelSize: Styles.Theme.fontSizeSmall
                elide: Text.ElideRight
            }

            Item { Layout.fillWidth: true }

            // 耗时
            Text {
                text: formatDuration(durationSeconds)
                color: Styles.Theme.textSecondary
                font.pixelSize: Styles.Theme.fontSizeSmall
                font.weight: Font.Medium
            }

            // 暂停标识
            Rectangle {
                visible: isPaused
                width: pauseText.width + 12
                height: 20
                radius: 10
                color: Qt.alpha(Styles.Theme.warning, 0.15)

                Text {
                    id: pauseText
                    anchors.centerIn: parent
                    text: qsTr("已暂停")
                    color: Styles.Theme.warning
                    font.pixelSize: 10
                    font.weight: Font.Medium
                }
            }
        }

        // 错误信息
        Rectangle {
            visible: errorMessage.length > 0
            Layout.fillWidth: true
            height: errorRow.height + Styles.Theme.spacingSmall * 2
            color: Qt.alpha(Styles.Theme.danger, 0.1)
            radius: Styles.Theme.radiusSmall
            border.color: Qt.alpha(Styles.Theme.danger, 0.3)
            border.width: 1

            RowLayout {
                id: errorRow
                anchors.fill: parent
                anchors.margins: Styles.Theme.spacingSmall
                spacing: Styles.Theme.spacingSmall

                Text {
                    text: "\u26A0"
                    font.pixelSize: 14
                    color: Styles.Theme.danger
                }

                Text {
                    text: errorMessage
                    color: Styles.Theme.danger
                    font.pixelSize: Styles.Theme.fontSizeSmall
                    wrapMode: Text.WordWrap
                    Layout.fillWidth: true
                }
            }
        }
    }

    // 辅助函数
    function statusColor(stat) {
        switch (stat) {
        case "completed": return Styles.Theme.success
        case "failed": return Styles.Theme.danger
        case "paused": return Styles.Theme.warning
        case "executing": return Styles.Theme.info
        default: return Styles.Theme.textSecondary
        }
    }

    function getStatusText() {
        switch (status) {
        case "completed": return qsTr("已完成")
        case "failed": return qsTr("失败")
        case "paused": return qsTr("暂停")
        case "executing": return qsTr("执行中")
        case "pending": return qsTr("等待中")
        default: return status
        }
    }

    function formatDuration(seconds) {
        if (seconds < 60) return seconds + "s"
        if (seconds < 3600) return Math.floor(seconds / 60) + "m " + (seconds % 60) + "s"
        var hours = Math.floor(seconds / 3600)
        var mins = Math.floor((seconds % 3600) / 60)
        return hours + "h " + mins + "m"
    }
}
